// ==UserScript==
// @name        	SteamGifts Addition
// @version     	1.0.1
// @namespace   	Liboicl
// @description    	Adds various features.
// @copyright      	2012 Kaitlyn <reowkaitlyn@gmail.com>, 2013 Liboicl <liboicl@gmail.com>
// @license        	MIT; http://en.wikipedia.org/wiki/Mit_license
// @include			http://www.steamgifts.com*
// @run-at        	document-end
// @grant			GM_xmlhttpRequest
// @icon 			
// @downloadURL		https://steamgifts-addition.googlecode.com/git/steamgifts-addition-latest-install.user.js
// @updateURL		https://steamgifts-addition.googlecode.com/git/steamgifts-addition-latest-install.user.js
// ==/UserScript==

function loadScript(tri, src){
	if(tri<10){
		GM_xmlhttpRequest({
			method: "GET",
			url: src,
			onload: function(response) {
				if(response.status != 200)
					loadScript(tri+1,src);
				else{
					var steamgiftsAddition = document.createElement('script');
					steamgiftsAddition.type = 'text/javascript';
					steamgiftsAddition.innerHTML = response.responseText;
					document.body.appendChild(steamgiftsAddition);
				}
			}
		});	
	}
}
if (window.top == window) {
	loadScript(0,'http://steamgifts-addition.googlecode.com/git/steamgifts-addition.js');
}

function wishlist(){
	id=unsafeWindow.sgaid;
    GM_xmlhttpRequest({
        method: "GET",
        url: "http://steamcommunity.com/profiles/"+id+"/wishlist",
        onload: function(response) {
            // Workaround the parser to satisfy chrome
			/*parser=new DOMParser();
			request=parser.parseFromString(request.responseText,"text/html");*/
			var request = document.implementation.createHTMLDocument("wishlist");
			request.documentElement.innerHTML = response.responseText;
            
            var wish=request.getElementById("wishlist_items").getElementsByClassName("wishlistRowItem");
            var games={};
            for(var i=0;i<wish.length;i++){
                games[wish[i].getElementsByTagName("h4")[0].innerHTML] = '';
            }
            unsafeWindow.games=games;
            location.href="javascript:lscache.set('giveawayWishlist',games);";
        }
    });
}

unsafeWindow.getWishlist=function() {
    setTimeout(wishlist, 0);
};
